// pages/shop/kanjia/index.js
const app = getApp();
let filter = require('../../../utils/intercept.js')
const http = require('../../../utils/http.js')
Page(filter.identityFilter({

  /**
   * 页面的初始数据
   */
  data: {
    txt:['再少也是肉','砍一刀是一刀，刀刀拿优惠','砍得越多，优惠越多','我也帮忙砍一刀'],
    textNum:{},
    coupon: {},
    userBool:true,
    memberId:''
  },
  goumaiBtn:function(){
    var that=this
    wx.showModal({
      title: '提示',
      content: '你是否确定购买该商品？',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.showLoading({
            title: '加载中',
            mask: true
          })
          http.get('/api/purchase/exposerUrl', {productId:that.data.id}).then(res1 => {
            wx.hideLoading()
            if (res1.code == 200) {
              http.get('/api/purchase/buyProduct/'+that.data.id, {md5:res1.data}).then(res => {
                wx.hideLoading()
                if (res.code == 200) {
                    //需要购买发起支付
                      let outTradeNo = res.data.outTradeNo
                      wx.requestPayment({
                          timeStamp: res.data.timeStamp,
                          nonceStr: res.data.nonceStr,
                          package: res.data.package,
                          signType: res.data.signType,
                          paySign: res.data.paySign,
                          success(res1) {
                            if (res1.errMsg = "requestPayment:ok") {
                              wx.showModal({
                                title: '购买成功',
                                content: '请去个人中心-我的优惠券中查看领取',
                                success (res2) {
                                  if (res2.confirm) {
                                    console.log('用户点击确定')
                                    wx.navigateTo({
                                      url: '/pages/user/mycoupon/index',
                                    })
                                  } else if (res2.cancel) {
                                    console.log('用户点击取消')
                                  }
                                }
                              })
                            }
                          }
                      })
                } else {
                    wx.showModal({
                        content: res.message,
                        showCancel: false,
                    })
                }
              })
            }else{
              wx.showModal({
                content: res1.data,
                showCancel: false,
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  friendBtn:function(){
    wx.showLoading({
      title: '加载中。。。',
      mask: true
    })
    http.get('/api/index/kanjia', {couponId: this.data.id,memberId:this.data.memberId}).then(res => {
      wx.hideLoading()
      if(res.code==200){
        this.myFunction(this.data.id,this.data.memberId)
      }
      wx.showToast({
        title: res.message,
        icon: 'none'
      })
    })
  },
  myFunction(id,memberId){
    var that=this
    http.get('/api/index/selectAndSaveKan', {couponId: id,memberId: memberId}).then(res => {
      if (res.code == 200) {
        //console.log(((res.result.amount-res.result.kanAmount).toFixed(2))*1)
        if(res.data.member.mobile != wx.getStorageSync("userInfo").mobile){
          this.setData({
            userBool:false
          })
        }
        this.setData({
          coupon: res.data.coupon,
          kanNum:(res.data.coupon.amount-res.data.coupon.bargainAmount).toFixed(2),
          zNum:(res.data.coupon.amount-res.data.couponMember.orderAmount).toFixed(2),
          kanjiaList:res.data,
          memberId:res.data.couponMember.memberId
        })
        const numList=[];
        for(var i=0;i<res.data.memberList.length;i++){
          numList.push(that.data.txt[Math.round(Math.random()*(that.data.txt.length-1))])
          that.setData({
            textNum:numList
          })
        }
      }
      //console.log(this.data.textNum)
      //console.log(res)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //var that=this
    //分享  还未接数据
    //console.log(options)
    if (options.memberId) {
      this.setData({
        memberId:options.memberId
      })
    }
    if (options.id) {
      this.setData({
        id:options.id
      })
      //this.myFunction(options.id,this.data.memberId);
    }
    
    this.setData({
      customer: app.globalData.customer
    })//.Math.round(Math.random()*4)
    //console.log(Math.round(Math.random()*4))
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.myFunction(this.data.id,this.data.memberId)
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if(res.from==='button'){
      //console.log(this.data.memberId)
      return {
        title: '“兄弟伙些快来帮我砍一刀，我要拿最低优惠”',
        //imageUrl: '/images/share.jpg',
        path: '/pages/shop/kanjia/index?id='+this.data.id+'&memberId='+this.data.memberId
      }
    }
  }
}))